﻿using PreeRegisterBLL;
using PreeRegisterModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            //Thread t1 = new Thread(new ThreadStart(PrintNumbers));//无参数的委托
            //t1.Start();

            Console.WriteLine("线程开始，同步检验报告的患者ID：" + "" + "\r\n\r\n");
            Thread thr1 = new Thread(new ParameterizedThreadStart(SynReportList));
            //thr1.Priority = ThreadPriority.AboveNormal;
            //thr1.IsBackground = true;
            thr1.Start("0946805-1");
            thr1.Name = "0946805-1";
            thr1.Join();
            Console.WriteLine("线程结束，同步检验报告的患者ID：" + "" + "\r\n\r\n");

            Thread t2 = new Thread(new ParameterizedThreadStart(PrintNumbers));//有参数的委托
            t2.Start(10);
            Console.ReadLine();
        }

        static void PrintNumbers()
        {
            Console.WriteLine("Starting...");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(i);
            }
        }

        //注意：要使用ParameterizedThreadStart，定义的参数必须为object
        static void PrintNumbers(object count)
        {
            Console.WriteLine("Starting2...");
            for (int i = 0; i < Convert.ToInt32(count); i++)
            {
                Console.WriteLine("Starting2" + i);
            }
        }

        /// <summary>
        /// 绑定关系接收成功后，推送该患者的检验报告
        /// </summary>
        /// <param name="pid">pid</param>
        private static void SynReportList(object pid)
        {
            string times = string.Empty;
            string patientCode = CommonBLL.GetAdnum(pid.ToString(), out times);
            ReportList(pid.ToString(), patientCode, times);
        }

        /// <summary>
        /// 检验报告推送
        /// </summary>
        /// <param name="pid">pid</param>
        /// <param name="patientCode">住院号</param>
        /// <param name="times">住院次数</param>
        private static void ReportList(string pid, string patientCode, string times)
        {
            int succ_count = 0;
            int error_count = 0;

            List<ReportListResponseModel> list = ReportBLL.GetReportList(patientCode, times);
            PreeRegisterBLL.hars_reportlist bll_reportlist = new PreeRegisterBLL.hars_reportlist();
            string RDSP_URL = VariableBLL.RDSP_IPAdress_INHOSPService + "/report/get";
            for (int i = 0; i <= list.Count; i++)
            {
                try
                {
                    ReportListResponseModel reportListResponseModel = list[i];
                    Thread.Sleep(1000);
                    //转换检验报告条目信息为json格式
                    string json_ReportList = csSerialize.ReportLsitResponseModel(reportListResponseModel);

                    //获取预约平台URL
                    ResponseModel responseModel = CommonBLL.SendInfo(RDSP_URL, json_ReportList);
                    if (responseModel.body.code == "1")
                    {
                        CommonBLL.WriteLog(string.Format("同步检验报告成功,PID：{0}，共{1}条，报告ID：{2}，第{3}条。",
                         pid, reportListResponseModel.pid, list.Count, reportListResponseModel.repid, i));
                        succ_count++;
                        bll_reportlist.Update(pid, reportListResponseModel.repid, "1", responseModel.body.msg);
                    }
                    else
                    {
                        CommonBLL.WriteLog(string.Format("同步检验报告失败,PID：{0}，共{1}条，报告ID：{2}，第{3}条。",
                         pid, reportListResponseModel.pid, list.Count, reportListResponseModel.repid, i));
                        error_count++;
                        //回置已发送标志
                        bll_reportlist.Update(pid, reportListResponseModel.repid, "2", responseModel.body.msg);
                    }
                }
                catch (Exception err)
                {
                    CommonBLL.WriteLog(string.Format("同步检验报告异常,异常信息：{0}。", err.ToString()));
                    Thread.CurrentThread.Abort();
                }
            }
            Thread.CurrentThread.Abort();
        }
    }
}
